package com.ruibang.glass.produce.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.produce.constant.Constants;
import com.ruibang.glass.produce.constant.ProduceErrorCode;
import com.ruibang.glass.produce.domain.req.CommonTrendChartReq;
import com.ruibang.glass.produce.domain.req.DefectAnalysisReq;
import com.ruibang.glass.produce.domain.resp.*;
import com.ruibang.glass.produce.entity.DefectDepthAnalysis;
import com.ruibang.glass.produce.mapper.DefectDepthAnalysisMapper;
import com.ruibang.glass.produce.service.DefectDepthAnalysisService;
import com.ruibang.glass.produce.util.DataUtil;
import com.teaming.cloud.framework2.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 缺陷深度数据分析 服务实现类
 * </p>
 *
 * @author songJinKang
 * @since 2024-04-27
 */
@Slf4j
@Service
public class DefectDepthAnalysisServiceImpl extends ServiceImpl<DefectDepthAnalysisMapper, DefectDepthAnalysis> implements DefectDepthAnalysisService {

    @Override
    public IPage<DefectDepthAnalysis> pageQuery(DefectAnalysisReq defectAnalysisReq) {
        QueryWrapper<DefectDepthAnalysis> qw = new QueryWrapper<>();
        if (defectAnalysisReq.getStartTime() != null || defectAnalysisReq.getEndTime() != null) {
            qw.lambda().ge(DefectDepthAnalysis::getCollectTime, defectAnalysisReq.getStartTime());
            qw.lambda().le(DefectDepthAnalysis::getCollectTime, defectAnalysisReq.getEndTime());
        }
        qw.lambda().orderByDesc(DefectDepthAnalysis::getCreateTime);
        return this.page(new Page<>(defectAnalysisReq.getPageNo(), defectAnalysisReq.getPageSize()), qw.lambda());

    }

    @Override
    public DefectDepthPieRateData pieRateChart(CommonTrendChartReq commonTrendChartReq) {
        //默认查询寻7天
        if (commonTrendChartReq.getStartTime() == null || commonTrendChartReq.getEndTime() == null) {
            LocalDateTime endTime = LocalDateTime.now();
            LocalDateTime startTime = endTime.minusDays(7);
            commonTrendChartReq.setStartTime(startTime);
            commonTrendChartReq.setEndTime(endTime);
        }
        return baseMapper.pieRateChart(commonTrendChartReq);
    }

    @Override
    public DefectDepthCurveRateData curveRateChart(CommonTrendChartReq commonTrendChartReq) {
        //默认查询寻7天
        if (commonTrendChartReq.getStartTime() == null || commonTrendChartReq.getEndTime() == null) {
            LocalDateTime endTime = LocalDateTime.now();
            LocalDateTime startTime = endTime.minusDays(7);
            commonTrendChartReq.setStartTime(startTime);
            commonTrendChartReq.setEndTime(endTime);
        }

        //获取数据
        List<DefectDepthAnalysis> data = lambdaQuery().ge(DefectDepthAnalysis::getCollectTime, commonTrendChartReq.getStartTime()).le(DefectDepthAnalysis::getCollectTime, commonTrendChartReq.getEndTime()).list();

        DefectDepthCurveRateData defectDepthCurveRateData = new DefectDepthCurveRateData();
        if (CollectionUtils.isNotEmpty(data)) {
            //采集时间
            List<String> collectTime = data.stream().map(e -> e.getCollectTime().format(Constants.DATE_TIME_FORMATTER)).collect(Collectors.toList());
            defectDepthCurveRateData.setCollectTime(collectTime);

            //bubble
            List<Double> bubble1 = data.stream().map(DefectDepthAnalysis::getBubbleRate1).filter(Objects::nonNull).collect(Collectors.toList());
            defectDepthCurveRateData.setBubbleRate1(bubble1);
            List<Double> bubble2 = data.stream().map(DefectDepthAnalysis::getBubbleRate2).filter(Objects::nonNull).collect(Collectors.toList());
            defectDepthCurveRateData.setBubbleRate2(bubble2);
            List<Double> bubble3 = data.stream().map(DefectDepthAnalysis::getBubbleRate3).filter(Objects::nonNull).collect(Collectors.toList());
            defectDepthCurveRateData.setBubbleRate3(bubble3);
            List<Double> bubble4 = data.stream().map(DefectDepthAnalysis::getBubbleRate4).filter(Objects::nonNull).collect(Collectors.toList());
            defectDepthCurveRateData.setBubbleRate4(bubble4);
            List<Double> bubble5 = data.stream().map(DefectDepthAnalysis::getBubbleRate5).filter(Objects::nonNull).collect(Collectors.toList());
            defectDepthCurveRateData.setBubbleRate5(bubble5);
            List<Double> bubble6 = data.stream().map(DefectDepthAnalysis::getBubbleRate6).filter(Objects::nonNull).collect(Collectors.toList());
            defectDepthCurveRateData.setBubbleRate6(bubble6);

            //stone
            List<Double> stone1 = data.stream().map(DefectDepthAnalysis::getStoneRate1).filter(Objects::nonNull).collect(Collectors.toList());
            defectDepthCurveRateData.setStoneRate1(stone1);
            List<Double> stone2 = data.stream().map(DefectDepthAnalysis::getStoneRate2).filter(Objects::nonNull).collect(Collectors.toList());
            defectDepthCurveRateData.setStoneRate2(stone2);
            List<Double> stone3 = data.stream().map(DefectDepthAnalysis::getStoneRate3).filter(Objects::nonNull).collect(Collectors.toList());
            defectDepthCurveRateData.setStoneRate3(stone3);
            List<Double> stone4 = data.stream().map(DefectDepthAnalysis::getStoneRate4).filter(Objects::nonNull).collect(Collectors.toList());
            defectDepthCurveRateData.setStoneRate4(stone4);
            List<Double> stone5 = data.stream().map(DefectDepthAnalysis::getStoneRate5).filter(Objects::nonNull).collect(Collectors.toList());
            defectDepthCurveRateData.setStoneRate5(stone5);
            List<Double> stone6 = data.stream().map(DefectDepthAnalysis::getStoneRate6).filter(Objects::nonNull).collect(Collectors.toList());
            defectDepthCurveRateData.setStoneRate6(stone6);


            //pt
            List<Double> pt1 = data.stream().map(DefectDepthAnalysis::getPtRate1).filter(Objects::nonNull).collect(Collectors.toList());
            defectDepthCurveRateData.setPtRate1(pt1);
            List<Double> pt2 = data.stream().map(DefectDepthAnalysis::getPtRate2).filter(Objects::nonNull).collect(Collectors.toList());
            defectDepthCurveRateData.setPtRate2(pt2);
            List<Double> pt3 = data.stream().map(DefectDepthAnalysis::getPtRate3).filter(Objects::nonNull).collect(Collectors.toList());
            defectDepthCurveRateData.setPtRate3(pt3);
            List<Double> pt4 = data.stream().map(DefectDepthAnalysis::getPtRate4).filter(Objects::nonNull).collect(Collectors.toList());
            defectDepthCurveRateData.setPtRate4(pt4);
            List<Double> pt5 = data.stream().map(DefectDepthAnalysis::getPtRate5).filter(Objects::nonNull).collect(Collectors.toList());
            defectDepthCurveRateData.setPtRate5(pt5);
            List<Double> pt6 = data.stream().map(DefectDepthAnalysis::getPtRate6).filter(Objects::nonNull).collect(Collectors.toList());
            defectDepthCurveRateData.setPtRate6(pt6);
        }
        return defectDepthCurveRateData;
    }

    @Override
    public List<DissolutionDefectData> defectChart(DefectAnalysisReq defectAnalysisReq) {
        if (defectAnalysisReq == null || StringUtils.isBlank(defectAnalysisReq.getType())) {
            throw new ServiceException(ProduceErrorCode.DEFECT_TYPE_IS_NULL.getCode(), ProduceErrorCode.DEFECT_TYPE_IS_NULL.getMsg());
        }
        //默认查询寻7天
        if (defectAnalysisReq.getStartTime() == null || defectAnalysisReq.getEndTime() == null) {
            LocalDateTime endTime = LocalDateTime.now();
            LocalDateTime startTime = endTime.minusDays(7);
            defectAnalysisReq.setStartTime(startTime);
            defectAnalysisReq.setEndTime(endTime);
        }
        return baseMapper.defectChart(defectAnalysisReq);
    }

    @Override
    public DefectDepthBarChartResp barChart(DefectAnalysisReq defectAnalysisReq) {
        if (defectAnalysisReq == null || StringUtils.isBlank(defectAnalysisReq.getType())) {
            throw new ServiceException(ProduceErrorCode.DEFECT_TYPE_IS_NULL.getCode(), ProduceErrorCode.DEFECT_TYPE_IS_NULL.getMsg());
        }
        //默认查询寻7天
        if (defectAnalysisReq.getStartTime() == null || defectAnalysisReq.getEndTime() == null) {
            LocalDateTime endTime = LocalDateTime.now();
            LocalDateTime startTime = endTime.minusDays(7);
            defectAnalysisReq.setStartTime(startTime);
            defectAnalysisReq.setEndTime(endTime);
        }
        List<DefectDepthAnalysis> defectDepthAnalysisList = baseMapper.barChart(defectAnalysisReq);
        DefectDepthBarChartResp defectDepthBarChartResp = new DefectDepthBarChartResp();
        //统计数据
        if (CollectionUtils.isNotEmpty(defectDepthAnalysisList)) {
            //采集时间
            List<String> collectTime = defectDepthAnalysisList.stream().map(e -> e.getCollectTime().format(Constants.DATE_TIME_FORMATTER)).collect(Collectors.toList());
            defectDepthBarChartResp.setCollectTime(collectTime);
            //缺陷数据统计
            switch (defectAnalysisReq.getType()) {
                case Constants.ONE_STR: {
                    //气泡
                    List<DefectDepthBarChartData> bubbleData = processBubbleBarChart(defectDepthAnalysisList);
                    defectDepthBarChartResp.setDefectDepthBarChartData(bubbleData);
                    break;
                }
                case Constants.TWO_STR: {
                    //铂金
                    List<DefectDepthBarChartData> ptData = processPtBarChart(defectDepthAnalysisList);
                    defectDepthBarChartResp.setDefectDepthBarChartData(ptData);
                    break;
                }
                case Constants.THREE_STR: {
                    //结石
                    List<DefectDepthBarChartData> stoneData = processStoneBarChart(defectDepthAnalysisList);
                    defectDepthBarChartResp.setDefectDepthBarChartData(stoneData);
                    break;
                }
                default: {
                    throw new ServiceException();
                }
            }
        }
        return defectDepthBarChartResp;
    }

    private List<DefectDepthBarChartData> processStoneBarChart(List<DefectDepthAnalysis> defectDepthAnalysisList) {
        List<DefectDepthBarChartData> data = new ArrayList<>();
        for (DefectDepthAnalysis defectDepthAnalysis : defectDepthAnalysisList) {
            DefectDepthBarChartData defectDepthBarChartData = new DefectDepthBarChartData();
            double checkTotalNum = Double.parseDouble(defectDepthAnalysis.getCheckTotalNum().toString());
            //0层
            if (defectDepthAnalysis.getStoneNum1() != null && defectDepthAnalysis.getStoneNum1() != 0) {
                defectDepthBarChartData.setData1(DataUtil.formatDouble(checkTotalNum / defectDepthAnalysis.getStoneNum1()));
            }
            //1-getStoneNum2
            if (defectDepthAnalysis.getStoneNum2() != null && defectDepthAnalysis.getStoneNum2() != 0) {
                defectDepthBarChartData.setData2(DataUtil.formatDouble(checkTotalNum / defectDepthAnalysis.getStoneNum2()));
            }
            //4-5层
            if (defectDepthAnalysis.getStoneNum3() != null && defectDepthAnalysis.getStoneNum3() != 0) {
                defectDepthBarChartData.setData3(DataUtil.formatDouble(checkTotalNum / defectDepthAnalysis.getStoneNum3()));
            }
            //6-8层
            if (defectDepthAnalysis.getStoneNum4() != null && defectDepthAnalysis.getStoneNum4() != 0) {
                defectDepthBarChartData.setData4(DataUtil.formatDouble(checkTotalNum / defectDepthAnalysis.getStoneNum4()));
            }
            //9层
            if (defectDepthAnalysis.getStoneNum5() != null && defectDepthAnalysis.getStoneNum5() != 0) {
                defectDepthBarChartData.setData5(DataUtil.formatDouble(checkTotalNum / defectDepthAnalysis.getStoneNum5()));
            }
            //10层
            if (defectDepthAnalysis.getStoneNum6() != null && defectDepthAnalysis.getStoneNum6() != 0) {
                defectDepthBarChartData.setData6(DataUtil.formatDouble(checkTotalNum / defectDepthAnalysis.getStoneNum6()));
            }
            data.add(defectDepthBarChartData);
        }
        return data;
    }

    private List<DefectDepthBarChartData> processPtBarChart(List<DefectDepthAnalysis> defectDepthAnalysisList) {
        List<DefectDepthBarChartData> data = new ArrayList<>();
        for (DefectDepthAnalysis defectDepthAnalysis : defectDepthAnalysisList) {
            DefectDepthBarChartData defectDepthBarChartData = new DefectDepthBarChartData();
            double checkTotalNum = Double.parseDouble(defectDepthAnalysis.getCheckTotalNum().toString());
            //0层
            if (defectDepthAnalysis.getPtNum1() != null && defectDepthAnalysis.getPtNum1() != 0) {
                defectDepthBarChartData.setData1(DataUtil.formatDouble(checkTotalNum / defectDepthAnalysis.getPtNum1()));
            }
            //1-3层
            if (defectDepthAnalysis.getPtNum2() != null && defectDepthAnalysis.getPtNum2() != 0) {
                defectDepthBarChartData.setData2(DataUtil.formatDouble(checkTotalNum / defectDepthAnalysis.getPtNum2()));
            }
            //4-5层
            if (defectDepthAnalysis.getPtNum3() != null && defectDepthAnalysis.getPtNum3() != 0) {
                defectDepthBarChartData.setData3(DataUtil.formatDouble(checkTotalNum / defectDepthAnalysis.getPtNum3()));
            }
            //6-8层
            if (defectDepthAnalysis.getPtNum4() != null && defectDepthAnalysis.getPtNum4() != 0) {
                defectDepthBarChartData.setData4(DataUtil.formatDouble(checkTotalNum / defectDepthAnalysis.getPtNum4()));
            }
            //9层
            if (defectDepthAnalysis.getPtNum5() != null && defectDepthAnalysis.getPtNum5() != 0) {
                defectDepthBarChartData.setData5(DataUtil.formatDouble(checkTotalNum / defectDepthAnalysis.getPtNum5()));
            }
            //10层
            if (defectDepthAnalysis.getPtNum6() != null && defectDepthAnalysis.getPtNum6() != 0) {
                defectDepthBarChartData.setData6(DataUtil.formatDouble(checkTotalNum / defectDepthAnalysis.getPtNum6()));
            }
            data.add(defectDepthBarChartData);
        }
        return data;
    }

    private List<DefectDepthBarChartData> processBubbleBarChart(List<DefectDepthAnalysis> defectDepthAnalysisList) {
        List<DefectDepthBarChartData> data = new ArrayList<>();
        for (DefectDepthAnalysis defectDepthAnalysis : defectDepthAnalysisList) {
            DefectDepthBarChartData defectDepthBarChartData = new DefectDepthBarChartData();
            double checkTotalNum = Double.parseDouble(defectDepthAnalysis.getCheckTotalNum().toString());
            //0层
            if (defectDepthAnalysis.getBubbleNum1() != null && defectDepthAnalysis.getBubbleNum1() != 0) {
                defectDepthBarChartData.setData1(DataUtil.formatDouble(checkTotalNum / defectDepthAnalysis.getBubbleNum1()));
            }
            //1-3层
            if (defectDepthAnalysis.getBubbleNum2() != null && defectDepthAnalysis.getBubbleNum2() != 0) {
                defectDepthBarChartData.setData2(DataUtil.formatDouble(checkTotalNum / defectDepthAnalysis.getBubbleNum2()));
            }
            //4-5层
            if (defectDepthAnalysis.getBubbleNum3() != null && defectDepthAnalysis.getBubbleNum3() != 0) {
                defectDepthBarChartData.setData3(DataUtil.formatDouble(checkTotalNum / defectDepthAnalysis.getBubbleNum3()));
            }
            //6-8层
            if (defectDepthAnalysis.getBubbleNum4() != null && defectDepthAnalysis.getBubbleNum4() != 0) {
                defectDepthBarChartData.setData4(DataUtil.formatDouble(checkTotalNum / defectDepthAnalysis.getBubbleNum4()));
            }
            //9层
            if (defectDepthAnalysis.getBubbleNum5() != null && defectDepthAnalysis.getBubbleNum5() != 0) {
                defectDepthBarChartData.setData5(DataUtil.formatDouble(checkTotalNum / defectDepthAnalysis.getBubbleNum5()));
            }
            //10层
            if (defectDepthAnalysis.getBubbleNum6() != null && defectDepthAnalysis.getBubbleNum6() != 0) {
                defectDepthBarChartData.setData6(DataUtil.formatDouble(checkTotalNum / defectDepthAnalysis.getBubbleNum6()));
            }
            data.add(defectDepthBarChartData);
        }
        return data;
    }
}
